package com.example.system01.service;


import com.example.system01.dao.DevicesDao;
import com.example.system01.entity.Devices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class DevicesService implements DevicesDao {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public int add(Devices devices) {
        String sql = "insert into devices values(null,?,?,?,?,?,'0')";
        // 2 调用方法实现
        Object[] args = {devices.getName(), devices.getPrice(),devices.getQuantity(), devices.getCategory(),devices.getSupplier()};
        return jdbcTemplate.update(sql, args);
    }

    @Override
    public int delete(Devices devices) {
        String sql = "delete from devices where Id = ?";
        // 2 调用方法实现
        Object[] args = {devices.getId()};
        return jdbcTemplate.update(sql, args);
    }

    @Override
    public int update(Devices devices) {
        String sql = "update devices set name=?,price=?,quantity=?,category=?,supplier=?where Id = ?";
        // 2 调用方法实现
        Object[] args = {devices.getName(),devices.getPrice(),devices.getQuantity(),devices.getCategory(),devices.getSupplier(),devices.getId()};
        return jdbcTemplate.update(sql, args);
    }

    @Override
    public Devices findOne(Devices devices) {
        String sql = "select * from devices where Id="+devices.getId().toString();
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(Devices.class));
    }

    @Override
    public List<Devices> findAll() {
        String sql = "select * from devices";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Devices.class));
    }
}
